package org.example.bingevent.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.apache.catalina.User;
import org.example.bingevent.common.Result;
import org.example.bingevent.mapper.RuleMapper;
import org.example.bingevent.mapper.UserMapper;
import org.example.bingevent.pojo.Rule;
import org.example.bingevent.pojo.Student;
import org.example.bingevent.pojo.Users;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Resource
    private UserMapper userMapper;

    @PostMapping("/login")
    public Result<?> login(@RequestBody Users users){
        //selectone 只接收一个数据
        Users users1 = userMapper.selectOne(new LambdaQueryWrapper<Users>().
                eq(Users::getUsername, users.getUsername())
                .eq(Users::getPassword, users.getPassword())
                .eq(Users::getRole, users.getRole())
        );
        if(users1 == null){
            return Result.error("-1","学号或密码错误");
        }
        return Result.success(users1); //会返回整个res 这样前端才可以把登录的用户名密码放入sessionstorage ！！！！！

    }
    @PostMapping("/reg") //用来登录
    public Result<?> reg(@RequestBody Users users){
        //selectone 只接收一个数据
        Users username = userMapper.selectOne(new QueryWrapper<Users>().eq("username", users.getUsername()));
        if(username != null){
            return Result.error("-1","账号重复");
        }
        users.setRole("STUDENT");
        userMapper.insert(users);
        return Result.success(); //会返回整个res 这样前端才可以把登录的用户名密码放入sessionstorage ！！！！！

    }


}

